package com.bro.person.admin.service.impl;

import com.bro.person.admin.model.AdminUser;
import com.bro.person.admin.utils.ValidateModelUtil;
import com.bro.person.base.model.paging.PagingResult;
import com.bro.person.base.service.impl.BroBaseServiceImpl;
import com.bro.person.admin.dao.mapper.AdminResourceMapper;
import com.bro.person.admin.model.AdminResource;
import com.bro.person.admin.model.AdminResourceCriteria;
import com.bro.person.admin.service.IAdminResourceService;

import com.bro.person.base.constants.ConstConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * AdminResource的 DB操作service
 *
 * @author jdd
 * @version 1.0
 * @date 2018-10-11 09:53:41
 */
@Service
@Transactional(rollbackFor={Exception.class})
public class AdminResourceServiceImpl extends BroBaseServiceImpl<AdminResource, AdminResourceCriteria, Integer> implements IAdminResourceService {

	private static Logger logger = LoggerFactory.getLogger(AdminResourceServiceImpl.class);

	@Resource
	@SuppressWarnings("SpringJavaAutowiringInspection")
	private AdminResourceMapper adminResourceMapper;

	@Override
	public List<AdminResource> queryAll(String orderByClause) {
		logger.debug("[AdminResourceServiceImpl][queryAll]entering, orderByClause:{}", orderByClause);
		AdminResourceCriteria criteria = new AdminResourceCriteria();
		criteria.createCriteria();
		criteria.setOrderByClause(orderByClause);
		return queryByCriteria(criteria);
	}

	@Override
	public List<AdminResource> fallloadByCriteria(int pageSize, AdminResourceCriteria criteria) {
		logger.debug("[AdminResourceServiceImpl][fallloadByCriteria]entering, pageSize:{}", pageSize);
		return null;
	}

	@Override
	public PagingResult<AdminResource> pagingByCriteria(int pageNo, int pageSize, AdminResourceCriteria criteria, AdminResource adminResource) {
		logger.debug("[AdminResourceServiceImpl][pagingByCriteria] entering, pageNo:{},pageSize:{}", pageNo, pageSize);
		// 确保pageNo合法
		pageNo = pageNo <= 0 ? 1 : pageNo;
		pageSize = pageSize <= 0 ? ConstConfig.PAGE_SIZE_DEFAULT : pageSize;
		// 确保pageSize合法
		int offset = (pageNo - 1) * pageSize;

		// 构造查询条件
		if (criteria == null) {
			criteria = new AdminResourceCriteria();
		}

		criteria.setLimitOffset(offset);
		criteria.setLimitRows(pageSize);
		logger.info("[AdminResourceServiceImpl][pagingByCriteria] offset:{}, pageNo: {},pageSize:{}", offset, pageNo, pageSize);
		int count = super.countByCriteria(criteria);
		List<AdminResource> dataList = adminResourceMapper.selectByExample(criteria);
		// 返回分页数据
		logger.info("[AdminResourceServiceImpl][pagingByCriteria] result, pageNo: {},pageSize:{},count:{},dataListSize:{}",
				pageNo, pageSize, count, dataList != null ? dataList.size() : 0);
		return new PagingResult<>(pageNo, pageSize, count, dataList);
	}

	@Override
	public AdminResourceMapper getBaseMapper() {
			return adminResourceMapper;
	}

	@Override
	public List<AdminResource> queryResourcesByUserId(AdminUser adminUser) {
		logger.debug("[AdminResourceServiceImpl][queryResourcesByUserId] entering adminUserId:{}", adminUser.getId());
		if(!ValidateModelUtil.isAdminUserValid(adminUser)){
			return new ArrayList<>();
		}
		List<AdminResource> resourceList;
		//写死
		if("admin".equals(adminUser.getUsername())){
			resourceList = super.queryAll();
		}else{
			resourceList = adminResourceMapper.queryResourcesByUserId(adminUser.getId(),adminUser.getStatus());
		}
		logger.info("[AdminResourceServiceImpl][queryResourcesByUserId] 获取当前用户对应的权限列表 adminUser:{},resourceListSize:{},resourceList:{}",adminUser,resourceList.size(),resourceList);
		return resourceList;
	}

	public static void main(String[] args) {
		System.out.println(hah("/sys/wyAdmin/232132/7d899d/leoadUserId"));
	}

	public static boolean hah(String treadUrl){
		boolean flag = false;
//		String permissionUrl = "/sys/wyAdmin/%/%/leoadUserId";
		return flag;
	}
}